import { defHttp } from '/@/utils/http/axios';
import { useMessage } from "/@/hooks/web/useMessage";

const { createConfirm } = useMessage();

enum Api {
	list = '/journey/bsTravelgroupTravelistgroup/list',
	getMsgsByGroup = '/journey/bsTravelgroupTravelistgroup/getMsgsByGroup',
	getMsgByPreline = '/journey/bsTravelgroupTravelistgroup/getMsgByPreline',
	save = '/journey/bsTravelgroupTravelistgroup/add',
	edit = '/journey/bsTravelgroupTravelistgroup/edit',
	deleteOne = '/journey/bsTravelgroupTravelistgroup/delete',
	deleteBatch = '/journey/bsTravelgroupTravelistgroup/deleteBatch',
	importExcel = '/journey/bsTravelgroupTravelistgroup/importExcel',
	exportXls = '/journey/bsTravelgroupTravelistgroup/exportXls',
}
/**
 * 导出api
 * @param params
 */
export const getExportUrl = Api.exportXls;
/**
 * 导入api
 */
export const getImportUrl = Api.importExcel;
/**
 * 列表接口
 * @param params
 */
export const list = (params) =>
	defHttp.get({ url: Api.list, params });

export const getMsgsByGroup = (params) =>
	defHttp.get({ url: Api.getMsgsByGroup, params });

export const getMsgByPreline = (params) =>
	defHttp.get({ url: Api.getMsgByPreline, params });
/**
 * 删除单个
 */
export const deleteOne = (params, handleSuccess) => {
	return defHttp.delete({ url: Api.deleteOne, params }, { joinParamsToUrl: true }).then(() => {
		handleSuccess();
	});
}
/**
 * 批量删除
 * @param params
 */
export const batchDelete = (params, handleSuccess) => {
	createConfirm({
		iconType: 'warning',
		title: '确认删除',
		content: '是否删除选中数据',
		okText: '确认',
		cancelText: '取消',
		onOk: () => {
			return defHttp.delete({ url: Api.deleteBatch, data: params }, { joinParamsToUrl: true }).then(() => {
				handleSuccess();
			});
		}
	});
}
/**
 * 保存或者更新
 * @param params
 */
export const saveOrUpdate = (params, isUpdate, isCopy) => {
	/* 	let startdate = new Date(params.startdate)
		let s = startdate.getTime() // 方法1
		let enddate = new Date(params.enddate)
		let e = enddate.getTime() // 方法1
		params.enddate = e
		params.startdate = s
	
		
	
	
		let dss = JSON.parse(params.travelcalendar);
		//组装开始日期和结束日期
	
		params.startdateStr = dss[0].markDate
	
		params.enddateStr = getNewData(dss[dss.length - 1].markDate, params.jDays - 1)*/

	params.images = params.images2

	let titlegroup = {
		mainTitle: params.mainTitle,
		secondTitle: params.secondTitle
	}

	params.titlegroup = JSON.stringify(titlegroup)
	let extInfor = {
		groupUserSize: params.groupUserSize,
		tagsTitle: params.tagsTitle,
		destination: params.destination,
		crossedPrice: params.crossedPrice,
		msg: params.msg,
		homeType: params.homeType,
		earlyBirdPrice: params.earlyBirdPrice,
		isEarly: params.isEarly,
		sjSelect: params.sjSelect,
		enddateStr: params.enddateStr,
		xjSelect: params.xjSelect,
		startdateStr: params.startdateStr,
		retrofitRule: params.retrofitRule,
		mustSee: params.mustSee,
		/**
		 * 还需要加成团价格，优惠价格，用户发起行程的阶段，已发起行程的日期
		 */
		payPrice: params.payPrice,
		stepprice: params.stepprice,
		userSelectDay: params.travelcalendar,
		travelcalendar: params.travelMarkDays

	}
	params.extinfor = JSON.stringify(extInfor);

	let url = isUpdate ? Api.edit : Api.save;
	if (isCopy) {
		url = Api.save;
	}


	return defHttp.post({ url: url, params });
}


function getNewData(dateTemp, days) {
	var dateTemp = dateTemp.split("-");
	var nDate = new Date(dateTemp[1] + '-' + dateTemp[2] + '-' + dateTemp[0]); //转换为MM-DD-YYYY格式    
	var millSeconds = Math.abs(nDate) + (days * 24 * 60 * 60 * 1000);
	var rDate = new Date(millSeconds);
	var year = rDate.getFullYear();
	var month = rDate.getMonth() + 1;
	if (month < 10) month = "0" + month;
	var date = rDate.getDate();
	if (date < 10) date = "0" + date;
	return (year + "-" + month + "-" + date);
}